home *** CD-ROM | disk | FTP | other *** search
/ MPEG Toolkit / MPEG Toolkit.iso / os2 / mpegenc / src / headers / frames.h < prev    next >
Encoding:
C/C++ Source or Header  |  1997-01-01  |  10.1 KB  |  315 lines

  1. /*===========================================================================*
  2.  * frames.h                                     *
  3.  *                                         *
  4.  *    stuff dealing with frames                         *
  5.  *                                         *
  6.  *===========================================================================*/
  7.  
  8. /*
  9.  * Copyright (c) 1993 The Regents of the University of California.
  10.  * All rights reserved.
  11.  *
  12.  * Permission to use, copy, modify, and distribute this software and its
  13.  * documentation for any purpose, without fee, and without written agreement is
  14.  * hereby granted, provided that the above copyright notice and the following
  15.  * two paragraphs appear in all copies of this software.
  16.  *
  17.  * IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY FOR
  18.  * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT
  19.  * OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF THE UNIVERSITY OF
  20.  * CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  21.  *
  22.  * THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES,
  23.  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
  24.  * AND FITNESS FOR A PARTICULAR PURPOSE.  THE SOFTWARE PROVIDED HEREUNDER IS
  25.  * ON AN "AS IS" BASIS, AND THE UNIVERSITY OF CALIFORNIA HAS NO OBLIGATION TO
  26.  * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
  27.  */
  28.  
  29. /*  
  30.  *  $Header: /n/picasso/users/keving/encode/src/headers/RCS/frames.h,v 1.5 1993/07/22 22:24:23 keving Exp keving $
  31.  *  $Log: frames.h,v $
  32.  * Revision 1.5  1993/07/22  22:24:23  keving
  33.  * nothing
  34.  *
  35.  * Revision 1.4  1993/07/09  00:17:23  keving
  36.  * nothing
  37.  *
  38.  * Revision 1.3  1993/06/03  21:08:53  keving
  39.  * nothing
  40.  *
  41.  * Revision 1.2  1993/03/02  19:00:27  keving
  42.  * nothing
  43.  *
  44.  * Revision 1.1  1993/02/19  20:15:51  keving
  45.  * nothing
  46.  *
  47.  */
  48.  
  49.  
  50. #ifndef FRAMES_INCLUDED
  51. #define FRAMES_INCLUDED
  52.  
  53. /*==============*
  54.  * HEADER FILES *
  55.  *==============*/
  56.  
  57. #include "ansi.h"
  58. #include "mtypes.h"
  59. #include "mheaders.h"
  60. #include "frame.h"
  61.  
  62.  
  63. /*===========*
  64.  * CONSTANTS *
  65.  *===========*/
  66.  
  67. #define I_FRAME    1
  68. #define P_FRAME 2
  69. #define    B_FRAME    3
  70.  
  71. #define LUM_BLOCK   0
  72. #define    CHROM_BLOCK 1
  73. #define    CR_BLOCK    2
  74. #define CB_BLOCK    3
  75.  
  76. #define    MOTION_FORWARD        0
  77. #define MOTION_BACKWARD        1
  78. #define MOTION_INTERPOLATE  2
  79.  
  80.  
  81. #define USE_HALF    0
  82. #define    USE_FULL    1
  83.  
  84.     /* motion vector stuff */
  85. #define FORW_F_CODE fCode        /* from picture header */
  86. #define BACK_F_CODE fCode
  87. #define FORW_F    (1 << (FORW_F_CODE - 1))
  88. #define    BACK_F    (1 << (BACK_F_CODE - 1))
  89. #define RANGE_NEG    (-(1 << (3 + FORW_F_CODE)))
  90. #define RANGE_POS    ((1 << (3 + FORW_F_CODE))-1)
  91. #define MODULUS        (1 << (4 + FORW_F_CODE))
  92.  
  93. #define ORIGINAL_FRAME    0
  94. #define DECODED_FRAME    1
  95.  
  96.  
  97. /*=======================*
  98.  * STRUCTURE DEFINITIONS *
  99.  *=======================*/
  100.  
  101. typedef    struct FrameTableStruct {
  102.     MpegFrame *frame;        /* this changes during execution */
  103.  
  104.     /* the following are all initted once and never changed */
  105.     /* (they depend only on the pattern */
  106.  
  107.     struct FrameTableStruct *next;
  108.     struct FrameTableStruct *prev;
  109.  
  110.     /* nextOutput is a pointer to next frame table entry to output */
  111.     struct FrameTableStruct *nextOutput;
  112.  
  113.     boolean    freeNow;    /* TRUE iff no frames point back to this */
  114.  
  115.     int number;
  116.  
  117.     int    bFrameNumber;        /* actual frame number, if a b-frame */
  118.     
  119. } FrameTable;
  120.  
  121.  
  122. /*==================*
  123.  * TYPE DEFINITIONS *
  124.  *==================*/
  125.  
  126.     /* none */
  127.  
  128.  
  129. void    EncodeYDC _ANSI_ARGS_((int32 dc_term, int32 *pred_term, BitBucket *bb));
  130. void EncodeCDC _ANSI_ARGS_((int32 dc_term, int32 *pred_term, BitBucket *bb));
  131.  
  132.  
  133. /*========*
  134.  * MACROS *
  135.  *========*/
  136.  
  137. #define FRAME_TYPE(num)        framePattern[num % framePatternLen]
  138.  
  139. /* return ceiling(a/b) where a, b are ints, using temp value c */
  140. #define int_ceil_div(a,b,c)   ((b*(c = a/b) < a) ? (c+1) : c)
  141. #define int_floor_div(a,b,c)    ((b*(c = a/b) > a) ? (c-1) : c)
  142.  
  143.  
  144. #define COMPUTE_BLOCK(block, qscale, n) {            \
  145.     Mpost_QuantZigBlock(block, fb[n], qscale, TRUE);    \
  146.     EncodeYDC(fb[n][0], &y_dc_pred, bb);            \
  147.     Mpost_RLEHuffIBlock(fb[n], bb);                \
  148.     }
  149.  
  150.  
  151.  
  152. /* assumes many things:
  153.  * block indices are (y,x)
  154.  * variables y_dc_pred, cr_dc_pred, and cb_dc_pred
  155.  * flat block fb exists
  156.  */
  157. #define    GEN_I_BLOCK(frameType, frame, bb, mbAI, qscale)    {            \
  158.     Mhead_GenMBHeader(bb,                            \
  159.             frameType /* pict_code_type */, mbAI /* addr_incr */,   \
  160.             0 /* mb_quant */, 0 /* q_scale */,                \
  161.             0 /* forw_f_code */, 0 /* back_f_code */,            \
  162.             0 /* horiz_forw_r */, 0 /* vert_forw_r */,            \
  163.             0 /* horiz_back_r */, 0 /* vert_back_r */,            \
  164.             0 /* motion_forw */, 0 /* m_horiz_forw */,            \
  165.             0 /* m_vert_forw */, 0 /* motion_back */,            \
  166.             0 /* m_horiz_back */, 0 /* m_vert_back */,            \
  167.             0 /* mb_pattern */, 1 /* mb_intra */);            \
  168.                                         \
  169.     /* Y blocks */                                \
  170.     COMPUTE_BLOCK(frame->y_blocks[y][x], qscale, 0);                \
  171.     COMPUTE_BLOCK(frame->y_blocks[y][x+1], qscale, 1);                \
  172.     COMPUTE_BLOCK(frame->y_blocks[y+1][x], qscale, 2);                \
  173.     COMPUTE_BLOCK(frame->y_blocks[y+1][x+1], qscale, 3);            \
  174.                                         \
  175.     /* CB block */                                \
  176.     Mpost_QuantZigBlock(frame->cb_blocks[y >> 1][x >> 1], fb[4], qscale, TRUE); \
  177.     EncodeCDC(fb[4][0], &cb_dc_pred, bb);                        \
  178.     Mpost_RLEHuffIBlock(fb[4], bb);                        \
  179.                                         \
  180.     /* CR block */                                \
  181.     Mpost_QuantZigBlock(frame->cr_blocks[y >> 1][x >> 1], fb[5], qscale, TRUE); \
  182.     EncodeCDC(fb[5][0], &cr_dc_pred, bb);                        \
  183.     Mpost_RLEHuffIBlock(fb[5], bb);                        \
  184.     }
  185.  
  186.  
  187. #define    BLOCK_TO_FRAME_COORD(bx1, bx2, x1, x2) {    \
  188.     x1 = (bx1)*DCTSIZE;                \
  189.     x2 = (bx2)*DCTSIZE;                \
  190.     }
  191.  
  192. #define MOTION_TO_FRAME_COORD(bx1, bx2, mx1, mx2, x1, x2) { \
  193.     x1 = (bx1)*DCTSIZE+(mx1);                \
  194.     x2 = (bx2)*DCTSIZE+(mx2);                \
  195.     }
  196.  
  197. #define COORD_IN_FRAME(fy,fx, type)                    \
  198.     ((type == LUM_BLOCK) ?                        \
  199.      ((fy >= 0) && (fx >= 0) && (fy < Fsize_y) && (fx < Fsize_x)) :    \
  200.      ((fy >= 0) && (fx >= 0) && (fy < Fsize_y/2) && (fx < Fsize_x/2)))
  201.  
  202. #define ENCODE_MOTION_VECTOR(x,y,xq, yq, xr, yr, f) {            \
  203.     int    tempC;                            \
  204.                                     \
  205.     if ( x < RANGE_NEG )        tempX = x + MODULUS;        \
  206.     else if ( x > RANGE_POS ) tempX = x - MODULUS;            \
  207.     else                    tempX = x;            \
  208.                                     \
  209.     if ( y < RANGE_NEG )        tempY = y + MODULUS;        \
  210.     else if ( y > RANGE_POS ) tempY = y - MODULUS;            \
  211.     else                    tempY = y;            \
  212.                                     \
  213.     if ( tempX >= 0 ) {                        \
  214.         xq = int_ceil_div(tempX, f, tempC);                \
  215.         xr = f - 1 + tempX - xq*f;                    \
  216.     } else {                            \
  217.         xq = int_floor_div(tempX, f, tempC);            \
  218.         xr = f - 1 - tempX + xq*f;                    \
  219.     }                                \
  220.                                     \
  221.     if ( tempY >= 0 ) {                        \
  222.         yq = int_ceil_div(tempY, f, tempC);                \
  223.         yr = f - 1 + tempY - yq*f;                    \
  224.     } else {                            \
  225.         yq = int_floor_div(tempY, f, tempC);            \
  226.         yr = f - 1 - tempY + yq*f;                    \
  227.     }                                \
  228.     }
  229.  
  230.  
  231. /*===============================*
  232.  * EXTERNAL PROCEDURE prototypes *
  233.  *===============================*/
  234.  
  235. void    ComputeBMotionLumBlock _ANSI_ARGS_((MpegFrame *prev, MpegFrame *next,
  236.                    int by, int bx, int mode, int fmy, int fmx,
  237.                    int bmy, int bmx, LumBlock motionBlock));
  238. int    BMotionSearch _ANSI_ARGS_((LumBlock currentBlock, MpegFrame *prev, MpegFrame *next,
  239.               int by, int bx, int *fmy, int *fmx, int *bmy, int *bmx, int oldMode));
  240.  
  241.  
  242. void    ComputeDiffDCTs _ANSI_ARGS_((MpegFrame *current, MpegFrame *prev, int by, int bx,
  243.             int my, int mx, int pattern));
  244. void    ComputeDiffDCTBlock _ANSI_ARGS_((Block current, Block motionBlock));
  245. void    ComputeMotionBlock _ANSI_ARGS_((uint8 **prev, int by, int bx, int my, int mx,
  246.                Block motionBlock));
  247. void    ComputeMotionLumBlock _ANSI_ARGS_((MpegFrame *prevFrame, int by,
  248.                        int bx, int my, int mx,
  249.                        LumBlock motionBlock));
  250. int32    ComputeBlockMAD _ANSI_ARGS_((Block current, Block prev));
  251.  
  252. void    GenIFrame _ANSI_ARGS_((BitBucket *bb, MpegFrame *mf));
  253. void    GenPFrame _ANSI_ARGS_((BitBucket *bb, MpegFrame *current, MpegFrame *prev));
  254. void    GenBFrame _ANSI_ARGS_((BitBucket *bb, MpegFrame *curr, MpegFrame *prev, MpegFrame *next));
  255.  
  256. void    ShowIFrameSummary _ANSI_ARGS_((int inputFrameBits, int32 totalBits, FILE *fpointer));
  257. void    ShowPFrameSummary _ANSI_ARGS_((int inputFrameBits, int32 totalBits, FILE *fpointer));
  258. void    ShowBFrameSummary _ANSI_ARGS_((int inputFrameBits, int32 totalBits, FILE *fpointer));
  259.  
  260.  
  261. /* DIFFERENCE FUNCTIONS */
  262.  
  263. int32    LumBlockMAD _ANSI_ARGS_((LumBlock currentBlock, LumBlock motionBlock, int32 bestSoFar));
  264. int32    LumBlockMSE _ANSI_ARGS_((LumBlock currentBlock, LumBlock motionBlock, int32 bestSoFar));
  265. int32    LumMotionError _ANSI_ARGS_((LumBlock currentBlock, MpegFrame *prev,
  266.                     int by, int bx, int my, int mx,
  267.                     int32 bestSoFar));
  268. int32    LumAddMotionError _ANSI_ARGS_((LumBlock currentBlock,
  269.                        LumBlock blockSoFar, MpegFrame *prev,
  270.                        int by, int bx, int my, int mx,
  271.                        int32 bestSoFar));
  272. int32    LumMotionErrorA _ANSI_ARGS_((LumBlock current, MpegFrame *prevFrame,
  273.                      int by, int bx, int my, int mx,
  274.                      int32 bestSoFar));
  275. int32    LumMotionErrorB _ANSI_ARGS_((LumBlock current, MpegFrame *prevFrame,
  276.                      int by, int bx, int my, int mx,
  277.                      int32 bestSoFar));
  278. int32    LumMotionErrorC _ANSI_ARGS_((LumBlock current, MpegFrame *prevFrame,
  279.                      int by, int bx, int my, int mx,
  280.                      int32 bestSoFar));
  281. int32    LumMotionErrorD _ANSI_ARGS_((LumBlock current, MpegFrame *prevFrame,
  282.                      int by, int bx, int my, int mx,
  283.                      int32 bestSoFar));
  284. int32    LumMotionErrorSubSampled _ANSI_ARGS_((LumBlock currentBlock, MpegFrame *prevFrame,
  285.               int by, int bx, int my, int mx,
  286.               int startY, int startX));
  287. void    ComputeSNR _ANSI_ARGS_((uint8 **origData, uint8 **newData,
  288.                 int ySize, int xSize,
  289.                 float *snr, float *psnr));
  290.  
  291.  
  292. /*==================*
  293.  * GLOBAL VARIABLES *
  294.  *==================*/
  295.  
  296. extern int pixelFullSearch;
  297. extern int searchRange;
  298. extern int qscaleI;
  299. extern int gopSize;
  300. extern int slicesPerFrame;
  301. extern int blocksPerSlice;
  302. extern FrameTable  *frameTable;
  303. extern int referenceFrame;
  304. extern int quietTime;        /* shut up for at least quietTime seconds;
  305.                  * negative means shut up forever
  306.                  */
  307.  
  308. extern boolean frameSummary;    /* TRUE = frame summaries should be printed */
  309. extern boolean    printSNR;
  310. extern boolean    decodeRefFrames;    /* TRUE = should decode I and P frames */
  311. extern int    fCode;
  312.  
  313.  
  314. #endif /* FRAMES_INCLUDED */
  315.